import React from "react";

import { ajax ,showResultMessage,isNotNull} from '../../common';

import {UIDataTableView} from '../../uicomponents/UIDataComponentView';

import UIFormView from '../../uicomponents/UIFormView';
/** 
 *课程科目
*/
export default class CourseItemDataTablePanel extends React.Component {

	constructor(props) {
		super(props);
		this.state = {
			formTitle: '',
		}
	}
	
	   
		
		
		
	/** Event Handle Code Example
	
	actionHandler = (actionEvent,panelInstance) => {

		return {
						        
			refresh : () => {
			},
									        
			addNew : () => {
			},
									        
			doDelete : () => {
			},
									
						
													
			doEdit : () => {
			},
																		
													
			doSave : () => {
			},
												
		};
	}
	
	
	*/

	actionHandler = (actionEvent) => {
	
	    const {actionHandler} = this.props;
	    
	    if(isNotNull(actionHandler)){
	        return actionHandler(actionEvent,this);
	    }
	    else{
	        return null;
	    }
	}
	
	/** 
	* 刷新课程科目列表
	*/
	loadData = (params) => {
		const { dataListRef } = this;
		if(isNotNull(this.props.onBeforeLoadData)){
			const loadParams = this.props.onBeforeLoadData();
			dataListRef.loadData({...params,...loadParams});
		}
		else{
			dataListRef.loadData(params);
		}
	}
	
	/** 
	* 新增课程科目
	*/
	addNew = () => {
		const { dataFormRef } = this;
		this.setState({ formTitle: '新增课程科目'});
		setTimeout(() => {
			dataFormRef.openModal();
		});
	}
	
	/** 
	* 编辑课程科目
	*/
	doEdit = (dataObject) => {
		const { dataFormRef } = this;
		this.setState({ formTitle: '编辑课程科目' });
		setTimeout(() => {
			dataFormRef.openModal(() => {
				dataFormRef.setFormData(dataObject);
			});
		});
	}
	
	/** 
	* 批量删除课程科目
	*/
	doDelete = (dataId) => {
	
		const { selectDataIds } = this;
	
		let selectedIds = null;

		if (isNotNull(dataId)) {
			selectedIds = [dataId];
		}
		else {
			selectedIds = selectDataIds;
		}

		ajax('EDUCATION/courseItem/batchDelete', { ids: selectedIds }, (data) => {
			this.loadData();
		});
	}
	
	/** 
	* 保存课程科目
	*/
	doSave = (params) => {
		
		const { dataFormRef } = this;
		
		dataFormRef.getFormData((formData) => {

			ajax('EDUCATION/courseItem/save', {...formData,...params}, (data) => {
				dataFormRef.closeModal();
				this.loadData();
				showResultMessage({
					success: true,
					message: 'message.saveSuccess',
				});
			});
		});
	}

	render() {

		const dataTableProps = {
			ref: (inst) => { this.dataListRef = inst },
			actionHandler: this.actionHandler,
			actionGroupId: 'ProductUI:courseItemListToolbar',
			rowActionGroupId: 'ProductUI:courseItemRowToolbar',
			tableConfigId: 'ProductUI:courseItemListTable',
			dataUrl: 'EDUCATION/courseItem/query',
			selectMode: 'multi',
			searchFormId: 'ProductUI:courseItemSearchForm',
			autoLoad: true,
			history : this.props.history,
			setToolbarState: (topToolBar, selectedIds) => {
				if (selectedIds.length > 0) {
					topToolBar.enable('delete');
					topToolBar.enable('edit');
				}
				else {
					topToolBar.disable('delete');
					topToolBar.disable('edit');
				}
			},
			onSelectChange:
				(selectRowKeys) => {
					this.selectDataIds = selectRowKeys;
				}
			,
		};

		const dataFormProps = {
			ref: (inst) => { this.dataFormRef = inst },
			formConfigId: 'ProductUI:courseItemForm',
			actionGroupId: 'ProductUI:saveActionGroup',
			actionHandler: this.actionHandler,
			inPop: true,
			destroyOnClose: true,
			title: this.state.formTitle,
			params : this.props.formParams,
		};

		return (
			<UIDataTableView {...dataTableProps}>
				<UIFormView {...dataFormProps} />
			</UIDataTableView>
		);
	}
}